package com.yangming.springboot.service;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yangming.springboot.dao.SaleChanceDao;
import com.yangming.springboot.dao.UserDao;
import com.yangming.springboot.enums.DevResult;
import com.yangming.springboot.enums.StateStatus;
import com.yangming.springboot.query.SaleChanceQuery;
import com.yangming.springboot.utils.AssertUtil;
import com.yangming.springboot.utils.PhoneUtil;
import com.yangming.springboot.vo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yangming.springboot.vo.SaleChance;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SaleChanceService {

    @Autowired
    private UserDao userDao;

    @Autowired
    private SaleChanceDao saleChanceDao;

    public SaleChance selectBySid(Integer sid){
        return saleChanceDao.queryById(sid);
    }

    public Map<String, Object> querySaleChancesByParams(SaleChanceQuery saleChanceQuery){
        Map<String, Object> map = new HashMap<String, Object>();
        PageHelper.startPage(saleChanceQuery.getPage(), saleChanceQuery.getLimit());
        PageInfo<SaleChance> pageInfo = new PageInfo<SaleChance>(userDao.selectByParams(saleChanceQuery));
        map.put("code", 0);
        map.put("msg", "");
        map.put("count", pageInfo.getTotal());
        map.put("data", pageInfo.getList());
        return map;
    }

    public void saveChance(SaleChance saleChance){
        /**
         * 1.参数校验
         * */
        checkParams(saleChance.getCustomerName(), saleChance.getLinkMan(), saleChance.getLinkPhone());
        saleChance.setState(StateStatus.UNSTATE.getType());
        saleChance.setDevResult(DevResult.UNDEV.getStatus());
        if (saleChance.getAssignMan() != ""){
            saleChance.setState(StateStatus.STATED.getType());
            saleChance.setDevResult(DevResult.DEVING.getStatus());
            saleChance.setAssignTime(new Date());
        }
        saleChance.setIsValid(1);
        saleChance.setCreateDate(new Date());
        System.out.println(new Date());
        System.out.println(new Date().getTime());
        saleChance.setUpdateDate(new Date());
        /**
         * inserSelective(saleChance);
         * 接下来的步骤就是把salechance对象使用saleDao这个数据库层接口把数据保存到数据库中，但是我没写，所以没有往下的步骤了
         * */
    }

    private void checkParams(String customerName, String linkMan, String linkPhone){
        AssertUtil.isTrue(customerName == null, "name error");
        AssertUtil.isTrue(linkMan == null, "link error");
        AssertUtil.isTrue(!(PhoneUtil.isMobile(linkPhone)), "linkphone error");
    }

    public void updateSaleChance(SaleChance saleChance){
        /*
        * 1.参数校验，
        * id存在
        * 用户名非空
        * linkman非空
        * linkphone非空
        * 2.参数设置
        * update时间跟新
        * 原始记录 未分配，修改后设置为已分配
        * state
        * assignTime
        * devResult
        * 三个值都可能变化
        * 原始记录 已分配 修改后为未分配
        * */
        User temp = userDao.queryById(saleChance.getId()); //这个地方应该单独写一个saleChanceDao来连接服务器查询的，我这里没写，不过意思是这个意思
        SaleChance temp1 = saleChanceDao.queryById(saleChance.getId());
        //然后这边因该写一个SaleChance salechance来接受这个从数据库获取的数据
        AssertUtil.isTrue(temp1 == null, "update records error");
        checkParams(saleChance.getCustomerName(), saleChance.getLinkMan(), saleChance.getLinkPhone());
        saleChance.setUpdateDate(new Date());
        //数据库记录中参数值不存在，但是跟新后参数值存在的情况
        if (temp1.getAssignMan() == null && saleChance.getAssignMan() != null){
            saleChance.setState(StateStatus.STATED.getType());
            saleChance.setAssignTime(new Date());
            saleChance.setDevResult(DevResult.DEVING.getStatus());
        }else if(temp1.getAssignMan() != null && saleChance.getAssignMan() == null){
            saleChance.setState(StateStatus.UNSTATE.getType());
            saleChance.setAssignTime(null);
            saleChance.setDevResult(DevResult.DEV_FAILED.getStatus());
            saleChance.setAssignMan("");
        }

        AssertUtil.isTrue(saleChanceDao.update(saleChance)<1, "chance date update error");

    }

    public void deleteSaleChance(Integer[] ids){
        AssertUtil.isTrue(ids.length == 0 || ids == null, "delete ids is empty");
        AssertUtil.isTrue(saleChanceDao.deleteBatch(ids) != ids.length, "record delete faliue");//可以删除的记录条数没有id数组中的那么多，那就是有问题的

    }

    public void updateSaleChanceDevResult(Integer id, Integer devResult){
        SaleChance temp = saleChanceDao.queryById(id);
        AssertUtil.isTrue(temp == null, "update devresult id error");
        temp.setDevResult(devResult);
        saleChanceDao.update(temp);
    }

}
